package com.example.demo.exception;

import com.example.demo.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice   //作用：用来捕获控制器controller层抛出的所有异常
// @ControllerAdvice
// @ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler({CustomException.class}) // 捕获自定义异常
    public Result handleCustomException(CustomException ex) {
        return ex.getResult(); // 返回封装好的错误结果
    }

    @ExceptionHandler     //指定处理何种异常
    public Result doException(Exception ex){
        log.error(ex.getMessage());
        return Result.error("出错了，请联系管理员！");
    }
}
